package com.easylive.admin.controller;

import com.easylive.entity.po.CategoryInfo;
import com.easylive.entity.query.CategoryInfoQuery;
import com.easylive.entity.request.category.AdminChangeSortReqVO;
import com.easylive.entity.request.category.AdminDelCategoryReqVO;
import com.easylive.entity.request.category.AdminSaveCategoryReqVO;
import com.easylive.entity.vo.ResponseVO;
import com.easylive.service.CategoryInfoService;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: yudaxian
 * @Description: 分类管理控制器
 * @DateTime: 2024/11/3 21:37
 **/
@RestController
@RequestMapping("/category")
public class CategoryController extends ABaseController {

    @Resource
    private CategoryInfoService categoryInfoService;

    /**
     * 查询分类信息
     * @return
     */
    @PostMapping("/loadCategory")
    public ResponseVO loadCategory() {
        // 先按照排序吗查询
        CategoryInfoQuery query = new CategoryInfoQuery();
        query.setOrderBy("sort asc");
        query.setConvert2Tree(true);
        List<CategoryInfo> list = categoryInfoService.findListByParam(query);

        return getSuccessResponseVO(list);
    }

    @PostMapping("/saveCategory")
    public ResponseVO saveCategory(@Validated AdminSaveCategoryReqVO reqVO) {

        CategoryInfo categoryInfo = new CategoryInfo();
        categoryInfo.setCategoryId(reqVO.getCategoryId());
        categoryInfo.setpCategoryId(reqVO.getPCategoryId());
        categoryInfo.setCategoryCode(reqVO.getCategoryCode());
        categoryInfo.setCategoryName(reqVO.getCategoryName());
        categoryInfo.setIcon(reqVO.getIcon());
        categoryInfo.setBackground(reqVO.getBackground());

        categoryInfoService.saveCategory(categoryInfo);

        return getSuccessResponseVO(null);
    }

    @PostMapping("/delCategory")
    public ResponseVO deleteCategory(@Validated AdminDelCategoryReqVO reqVO) {

        categoryInfoService.delCategory(reqVO.getCategoryId());

        return getSuccessResponseVO(null);
    }

    @PostMapping("/changeSort")
    public ResponseVO changeSort(@Validated AdminChangeSortReqVO reqVO) {

        categoryInfoService.changeSort(reqVO.getPCategoryId(), reqVO.getCategoryIds());

        return getSuccessResponseVO(null);
    }
}
